import http from "@/utils/request"
import { WIDGET } from "@/components/flowable/flow/common/FlowConstant";
import config from "@/config";

async function listGroups(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/listGroups",params,{});
}

async function listGroupsWithFlowDefinition(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/listGroupsWithFlowDefinition",params,{});
}

async function listGroupsWithEnabledFlowDefinition(params) {
	params = { isEnable: 1 }
	return await http.post(config.API_URL+"/console/flowableEventManage/listGroupsWithFlowDefinition",params,{});
}

async function saveOrUpdateGroup(data) {
	return await http.post(config.API_URL+"/console/flowableEventManage/saveOrUpdateGroup",data,{});
}

async function deleteGroup(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/deleteGroup",params,{});
}

async function listFlowDefinitions(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/listFlowDefinitions",params,{});
}

async function getFlowConfig(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/getFlowConfig",params,{}).then((resp) => {
		let flowDefJson = {};
		if (resp.code == 2000) {
			flowDefJson = JSON.parse(resp.data.flowDefJson);
			let { workFlowDef } = flowDefJson;
			workFlowDef.id = resp.data.flowDefId;
		}
		return flowDefJson;
	});
}



async function saveOrUpdate(data) {
	return await http.post(config.API_URL+"/console/flowableEventManage/saveForm",data,{});
}

async function removeById(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/removeById",params,{});
}

async function freezeById(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/freezeById",params,{});
}

async function enableById(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/enableById",params,{});
}

async function getFlowForm(params) {
	return await http.post(config.API_URL+"/console/flowableEventManage/getFlowForm",params,{});
}

function formWidgetListToMap(flowWidgets) {
    let flowWidgetMap = {};
    flowWidgets.forEach((flowWidget) => {
        let { name, type, details } = flowWidget;
        flowWidgetMap[name] = flowWidget;
        if (type == WIDGET.DETAIL) details.forEach((detail) => (flowWidgetMap[detail.name] = detail));
    });
    return flowWidgetMap;
}

export default {
    listGroups,
	listGroupsWithFlowDefinition,
	listGroupsWithEnabledFlowDefinition,
    saveOrUpdateGroup,
    deleteGroup,
	listFlowDefinitions,
    getFlowConfig,
    removeById,
    saveOrUpdate,
    freezeById,
    enableById,
    getFlowForm,
    formWidgetListToMap,
};
